package org.kernely.security.ui.group.mgmt;

import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

import org.kernely.security.dao.RoleDAO;
import org.kernely.security.persistence.Group;
import org.kernely.security.persistence.Role;
import org.kernely.ui.mvp.KernelyPresenter;

import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.ui.CheckBox;

public class EditGroupRolePresenter extends KernelyPresenter<EditGroupRoleView>{

	/**
	 * The group to edit in edition mode
	 */
	private Group group;
	/**
	 * The set containing all the new roles of the group
	 */
	private Set<Role> groupRoles = new HashSet<Role>();
	
	private boolean editMode = false;
	
	/**
	 * Default constructor in creation mode
	 */
	public EditGroupRolePresenter(){}
	
	/**
	 * Constructor for the edition mode
	 * @param group : the group to edit
	 */
	public EditGroupRolePresenter(Group group){
		this.group = group;
		this.editMode = true;
	}
	
	/**
	 * @see org.kernely.ui.mvp.IKernelyPresenter#bind()
	 */
	@Override
	public final void bind() {
		// In edition mode, loading of existing roles for this group
		if(editMode){
			groupRoles.addAll(group.getRoles());
		}

		RoleDAO rdao = RoleDAO.getInstance();
		Set<Role> allRoles = rdao.getAllRoles();
		TreeSet<Role> sorted = new TreeSet<Role>(allRoles);
		
		for(Role r : sorted){
			final RoleCheckBox rcb;
			if(groupRoles.contains(r)){
				rcb = new RoleCheckBox(r.getName(), true, r);
			}
			else{
				rcb = new RoleCheckBox(r.getName(), false, r);
			}
			rcb.addListener(new ValueChangeListener(){
				private static final long serialVersionUID = -2761797639733607828L;

				@Override
				public void valueChange(com.vaadin.data.Property.ValueChangeEvent event) {
					if((Boolean)rcb.getValue()){
						groupRoles.add(rcb.getAssociatedRole());
					}
					else{
						groupRoles.remove(rcb.getAssociatedRole());
					}
				}
				
			});
			view.getRoleLayout().addComponent(rcb);
		}
		
	}

	/**
	 * Gets the set containing the roles of the new group
	 * @return the final roles of the group
	 */
	public final Set<Role> getGroupRoles(){
		return this.groupRoles;
	}
	
	/**
	 * RoleCheckBox extending CheckBox class
	 * Used to store the ID of the role corresponding
	 */
	private class RoleCheckBox extends CheckBox{
		private static final long serialVersionUID = -5754594621339389249L;
		
		/**
		 * Role associated to this checkbox
		 */
		private Role associatedRole;
		
		/**
		 * Default constructor
		 * @param caption : CheckBox's caption
		 * @param defaultValue : CheckBox's default value : true or false
		 * @param role : role associated to this checkBox
		 */
		public RoleCheckBox(String caption,boolean defaultValue, Role role){
			this.setCaption(caption);
			this.setValue(defaultValue);
			this.setImmediate(true);
			this.associatedRole = role;
		}
		
		/**
		 * Gets the associated role of this checkbox
		 * @return : the associated role
		 */
		public final Role getAssociatedRole(){
			return this.associatedRole;
		}
	}
}
